package com.hqyj.fj.javaSpringBoot.modules.account.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.fj.javaSpringBoot.modules.account.entity.Resource;
import com.hqyj.fj.javaSpringBoot.modules.account.service.ResourceService;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.fj.javaSpringBoot.modules.common.entity.SearchBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Page;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /*
     * 127.0.0.1/api/resource ---- post
     * {"permit":"/test"}
     */
    @PostMapping(value = "/resource", consumes = "application/json")
    public Result<Resource> insertResource(@RequestBody Resource resource) {
        return resourceService.insertResource(resource);
    }

    /*
     * 127.0.0.1:8081/api/resource/insert ---- post
     * {"permit":"/api"}
     */
    @PostMapping(value = "/resource/insert", consumes = "application/json")
    //自己写的接口但是和前端数据不同
    public Result<Resource> insertResourceMy(@RequestBody Resource resource){
        return resourceService.editResource(resource);
    }

    /*
     * 127.0.0.1/api/resource ---- put
     * {"id":"1","permission":"deleteUser"}
     */
    @PutMapping(value = "/resource", consumes = "application/json")
    public Result<Resource> updateResource(@RequestBody Resource resource) {
        return resourceService.updateResource(resource);
    }


    //自己用jpa 写的接口
    /*
     * 127.0.0.1:8081/api/resource/update ---- put
     * {id=4,"permit":"/api/resource"}
     */
    @PutMapping(value = "/resource/update", consumes = "application/json")
    public Result<Resource> updateResourceMy(@RequestBody Resource resource){
        return resourceService.editResource(resource);
    }

    /*
    * 127.0.0.1:8081/api/resource/7 delete
    * */
    @DeleteMapping(value = "/resource/{id}")
    public Result<Object> deleteResourceById(@PathVariable int id){
        return resourceService.deleteResourceById(id);
    }

    /*
     * 127.0.0.1:8081/api/resource/1 get
     * */
    @GetMapping(value = "/resource/{id}")
    public Resource getResourceById(@PathVariable int id){
        return resourceService.getResourceById(id);
    }

    /*
    *  127.0.0.1:8081/api/resource?permit=/api/resource
    * */
    @GetMapping("/resource")
    public List<Resource> getResourceByPermit(@RequestParam String permit){
        return  resourceService.getResourceByPermit( permit);
    }

    /*
     * 127.0.0.1:8081/api/resource ---- post
     * {"currentPage":1,"pageSize":2,"orderBy":"id","direction":"asc","keyword":""}
     */
    @PostMapping(value = "/resources", consumes = "application/json")
    public Page<Resource> getResourcesBySearchBean(@RequestBody SearchBean searchBean){
        return resourceService.getResourceBySearchBean(searchBean);
    }

    /*
     * 127.0.0.1:8081/api/resources/v2 ---- post
     * [{"permit":"/user"},
     * {"permit":"/role"}]
     */
    @PostMapping(value = "/resources/v2",consumes = "application/json")
    public Result<Object> insertUsers(@RequestBody  List<Resource> resourceList){
        return resourceService.insertResources(resourceList);
    }
}
